<template>
    <div class="block" style="margin-left: 5%;margin-top: 5%">
      <label>报表下载</label>
      <el-date-picker
        v-model="date"
        type="daterange"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      style="margin-left: 2%">
      </el-date-picker>
      <el-button type="success" icon="el-icon-download"  @click="download()"> 下载</el-button>

    </div>

</template>

<script>
import { accountant_download } from '../../api/other'
import axios from 'axios'
export default {
  name: 'daochu',
  computed: {
    // ...mapGetters([
    //   'name'
    // ])
  },
  data(){
    return{
      date:[],
    }
  },
  mounted() {
  },
  methods:{
    download(){
      let params = {
        filename: this.date,
        // filename: "accountant.xlsx",
      }
      // console.log(params)
      return new Promise((resolve, reject) => {
        // console.log(`${url} 请求数据，参数=>`, JSON.stringify(options))
        // axios.defaults.headers['content-type'] = 'application/json;charset=UTF-8'
        // console.log(options)
        axios({
          method: 'post',
          // url: 'http://127.0.0.1:8006/api/download/excel/', // 请求地址
          url: 'http://wmss.jasonsong.top/api/download/excel/', // 请求地址
          data: params, // 参数
          responseType: 'blob' // 表明返回服务器返回的数据类型
        })
          .then(
            response => {
              // console.log("下载响应",response)
              resolve(response.data)
              let blob = new Blob([response.data], {
                type: 'application/vnd.ms-excel'
              })
              // console.log(blob)
              // let fileName = Date.parse(new Date()) + '.xlsx'
              // 切割出文件名
              let fileNameEncode = response.headers['content-disposition'].split("filename=")[1];
              // 解码
              let fileName = decodeURIComponent(fileNameEncode)
              // console.log("fileName",fileName)
              if (window.navigator.msSaveOrOpenBlob) {
                // console.log(2)
                navigator.msSaveBlob(blob, fileName)
              } else {
                // console.log(3)
                var link = document.createElement('a')
                link.href = window.URL.createObjectURL(blob)
                link.download = fileName
                link.click()
                //释放内存
                window.URL.revokeObjectURL(link.href)
              }
            },
            err => {
              reject(err)
            }
          )
      })

    },

  }

}
</script>


